window.onload = function() {
	$(".nav li").hover(function() {
		$(this).find("ul").show();
	}, function() {
		$(this).find("ul").hide();
	});

	$(window).scroll(function() {
		var scrollTop = $(this).scrollTop();
		if(scrollTop > 100) {
			$("#Mainav").addClass("navbar");
		}
		if(scrollTop < 100) {
			$("#Mainav").removeClass("navbar")
		}
	});

	/******轮播*****/
	var index = 0;
	var Timer = setInterval(autoPlay, 2000);

	function autoPlay() {
		index++;
		if($("#box li").size() == index) {
			index = 0;
		}
		$("#box li").eq(index).show().siblings().hide();
		$("ol li").eq(index).addClass("current").siblings().removeClass("current");
	}
	$("ol li").mouseover(function() {
		clearInterval(Timer);
		index = $(this).index() - 1;
		autoPlay();
	}).mouseout(function() {
		Timer = setInterval(autoPlay, 2000);
	})
	$(".shop_now").hover(function() {
		clearInterval(Timer);
	}, function() {
		Timer = setInterval(autoPlay, 2000);
	})

	$(".collect_top").hover(function() {
		$(this).find("ul").show();
	}, function() {
		$(this).find("ul").hide();
	})

	/*************倒计时*************/
	var endTime = "2018/10/24 14:57:00";
	var endDate = new Date(endTime);
	var nowDate = new Date();

	var dif = timeDifferent(nowDate, endDate);
	//alert(dif);

	function showTime() {
		//如果时间差小于0表示商品过期
		if(dif < 0) {
			$id("showDate").innerHTML = "商品已经过期了";
			return;
		}
		var d = parseInt(dif / 3600 / 24); //得到天数
		var h = parseInt((dif / 3600 / 24 - d) * 24); //得到剩余的小时
		var m = parseInt(((dif / 3600 / 24 - d) * 24 - h) * 60); //得到剩余的分钟数；
		var s = parseInt((((dif / 3600 / 24 - d) * 24 - h) * 60 - m) * 60); //得到剩余的秒数
		$id("day").innerHTML = d + "天";
		$id("hour").innerHTML = h + "时";
		$id("minite").innerHTML = m + "分";
		$id("second").innerHTML = s + "秒";
	}
	showTime();
	var timer = setInterval(function() {
		dif--
		if(dif < 0) {
			$id("showDate").innerHTML = "商品已经过期了";
			clearInterval(timer);
		}
		showTime();
	}, 1000);
	/****************获取页面数据******************/
	//	collection
	$.ajax({
		type: "get",
		url: "index.php",
		success: function(res) {
			var html = "";
			var htmll = "";
			var wedding = "";
			var htmlt="";
			var arr = JSON.parse(res);
			for(var i = 0; i < 4; i++) {
				html += `
					<li class="collect_top">
	    				<a href="detail.html" class="collec">
	    					<img src="images/${arr[i].url}" width="100%"/>
	    					
	    				</a>
	    				<ul class="icon">
	    						<li><a href="detail.html"><i class="iconfont icon-yanjing"></i></a></li>
			    				<li><a href="#"><i class="iconfont icon-lanzi"></i></a></li>
			    				<li><a href="#"><i class="iconfont icon-xin"></i></a></li>
	    					</ul>
	    				<a href="#" class="fname">${arr[i].name}</a>
	    				<span id="">
	    					${arr[i].assess}
	    				</span>
	    				<span>$${arr[i].price}</span>
	    				<a href="javascript:;" class="shopcar">加入购物车</a>
	    			</li>`
			}
			for(var i = 4; i < 8; i++) {
				htmll += `
					<li class="collect_top wedding_top">
	    				<a href="detail.html" class="collec">
	    					<img src="images/${arr[i].url}" width="100%"/>
	    					
	    				</a>
	    				<ul class="icon">
	    						<li><a href="detail.html"><i class="iconfont icon-yanjing"></i></a></li>
			    				<li><a href="#"><i class="iconfont icon-lanzi"></i></a></li>
			    				<li><a href="#"><i class="iconfont icon-xin"></i></a></li>
	    					</ul>
	    				<a href="#" class="fname">${arr[i].name}</a>
	    				<span id="">
	    					${arr[i].assess}
	    				</span>
	    				<span>$${arr[i].price}</span>
	    				<a href="javascript:;" class="shopcar">加入购物车</a>
	    			</li>`
			}
			for(var i = 8; i < 12; i++) {
				htmlt += `
					<li class="collect_top holiday_top">
	    				<a href="detail.html" class="collec">
	    					<img src="images/${arr[i].url}" width="100%"/>
	    					
	    				</a>
	    				<ul class="icon">
	    						<li><a href="detail.html"><i class="iconfont icon-yanjing"></i></a></li>
			    				<li><a href="#"><i class="iconfont icon-lanzi"></i></a></li>
			    				<li><a href="#"><i class="iconfont icon-xin"></i></a></li>
	    					</ul>
	    				<a href="#" class="fname">${arr[i].name}</a>
	    				<span id="">
	    					${arr[i].assess}
	    				</span>
	    				<span>$${arr[i].price}</span>
	    				<a href="javascript:;" class="shopcar">加入购物车</a>
	    			</li>`
			}
			$(".holiday_left").html(htmlt);
			
			$(".wedding").html(htmll);

			$(".collect_right").html(html);

			/********添加到购物车*******/
			new addCar(arr);

		}

	});
	/**************购物车****************/
	function addCar(arr) {
		$(".shopcar").click(function() {
			var fname=$(this).parent().find(".fname").html();
			var goodsInfo=[{
				"fname":fname,
				"count":1
			}]
			var goodsInfoJson = JSON.stringify(goodsInfo);
			if(!localStorage.getItem(uname+"GoodsInfo")){
				//如果不存在就创建
				localStorage.setItem(uname+"GoodsInfo",goodsInfoJson);
			}else{
				//购物车信息如果存在就添加
				var goodsInfo=localStorage.getItem(uname+"GoodsInfo");
				var goodsInfoJson=JSON.parse(goodsInfo);
				var flag=true;
				for(var i=0;i<goodsInfoJson.length;i++){
					if(goodsInfoJson[i].fname==fname){
						goodsInfoJson[i].count++;
						flag=false;
					}
				}
				if(flag){
					var goodObj = {"fname":fname,"count":1};
					goodsInfoJson.push(goodObj);
				}
				var json = JSON.stringify(goodsInfoJson);
				localStorage.setItem(uname+"GoodsInfo",json);
			}
			showCartNum(arr);
		})
	}
	function showCartNum(arr) {
		var goodsInfo=localStorage.getItem(uname+"GoodsInfo");
		if(goodsInfo){
			var goodsInfoJson = JSON.parse(goodsInfo);
			var sum=0;
			
			for(var i=0;i<goodsInfoJson.length;i++){
				
				console.log(goodsInfoJson[i].count)
				sum+=parseInt(goodsInfoJson[i].count);
			}
			$("#totalCount").html(sum);
			addInfo(arr);
		}
	}
function addInfo(arr) {
	var goodsInfo = localStorage.getItem(uname+"GoodsInfo");
	if(goodsInfo){
		var goodsInfoJson = JSON.parse(goodsInfo);
		var str="";
		
		for(var i=0;i<goodsInfoJson.length;i++){
			
			for(var j=0;j<arr.length;j++){
				if(goodsInfoJson[i].fname==arr[j].name){
					str+=`<dl>
									<dt><img src="images/${arr[j].url}" width="60px"/></dt>
									<dd><p><span>名字</span><span>${arr[j].name}</span></p>
										<p><span>单价</span><span>${arr[j].price}</span></p>
										<p><span>数量</span><span>${goodsInfoJson[i].count}</span></p>
										<p><span>总价</span><span>${Math.round(arr[j].price*goodsInfoJson[i].count)}</span></p>
									</dd>
								</dl>`
				}
			}
		}
		$(".goods").html(str);
	}
}






	/*****************购物车显示**************/
	$("#shopcar").hover(function() {
		$(this).find(".goods").show();
	}, function() {
		$(this).find(".goods").hide();
	});
	
	
	
	//判断用户是否登录
	var uname = localStorage.getItem("uname");
	if(uname){
		$(".login").html(uname);
	}
	

}